package Exc.controller;


import Exc.domain.*;
import Exc.model.SendMail;
import Exc.service.IIpCountService;
import Exc.service.IIpMessageService;
import Exc.service.IIpMonitoringService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;

/**
 * The type Ip message controller.
 * @author fengke
 */
@Controller
@RequestMapping("/ip")
public class IpMessageController {
    @Autowired
    private IIpMessageService ipMessageService;
    @Autowired
    private IIpCountService iIpCountService;
    //监控接口
    @Autowired
    private IIpMonitoringService iIpMonitoringService;

    /**
     * 所有ip信息
     *
     * @param queryObject the query object
     * @return json result
     */
    @RequestMapping("/listIpMessage")
    @ResponseBody
    public JSONResult listIpMessage(QueryObject queryObject) {
        PageResult<IPMessage> ipMessageList = ipMessageService.queryIpMessageByPage(queryObject);
        JSONResult jsonResult = new JSONResult(true, "返回IP成功", ipMessageList);
        return jsonResult;
    }

    /**
     * fromIp count统计
     *
     * @return from ip count
     */
    @RequestMapping("/getFromIp")
    @ResponseBody
    public JSONResult getFromIpCount() {
        List<IpCount> ipCountList = iIpCountService.getFromIpCount();
        JSONResult jsonResult = new JSONResult(true, "Get FromIpCount Success", ipCountList);
        return jsonResult;
    }

    /**
     * destinationIp count统计
     *
     * @return destination ip count
     */
    @RequestMapping("/getDestinationIp")
    @ResponseBody
    public JSONResult getDestinationIpCount() {
        List<IpCount> ipCountList = iIpCountService.getDestinationIpCount();
        JSONResult jsonResult = new JSONResult(true, "Get DestinationIpCount Success", ipCountList);
        return jsonResult;
    }

    /**
     * 目的端口
     *
     * @return destination port count
     */
    @RequestMapping("/getDestinationPort")
    @ResponseBody
    public JSONResult getDestinationPortCount() {
        List<IpCount> ipCountList = iIpCountService.getDestinationPortCount();
        JSONResult jsonResult = new JSONResult(true, "Get DestinationPortCount Success", ipCountList);
        return jsonResult;
    }

    @RequestMapping("/getFlowCount")
    @ResponseBody
    public JSONResult getFlowCount() {
        List<IpCount> ipCountList = iIpCountService.getFlowCount();
        JSONResult jsonResult = new JSONResult(true, "Get FlowCount Success", ipCountList);
        return jsonResult;
    }

    @RequestMapping("/getIpAndPortCount")
    @ResponseBody
    public JSONResult getIpAndPortCount() throws Exception {
        List<IpAndPortCount> ipAndPortCount = iIpMonitoringService.getIpAndPortCount();
        if (ipAndPortCount.get(0).getDST_PORT() - ipAndPortCount.get(0).getSRC_IP() >= 1000) {
            SendMail sendMail = new SendMail();
            sendMail.SendMail("发现疑似面向主机端口扫描");
        }
        JSONResult jsonResult = new JSONResult(true, "获取Ip和Port数目成功", ipAndPortCount);
        return jsonResult;
    }

    @RequestMapping("/getPortAndIpCount")
    @ResponseBody
    public JSONResult getPortAndIpCount() throws Exception {
        List<PortAndIpCount> ipAndPortCount = iIpMonitoringService.getPortAndIpCount();
        if (ipAndPortCount.get(0).getDST_IP() - ipAndPortCount.get(0).getDST_PORT() >= 100) {
            SendMail sendMail = new SendMail();
            sendMail.SendMail("发现疑似面向网络的端口扫描");
        }
        JSONResult jsonResult = new JSONResult(true, "获取Ip和Port数目成功", ipAndPortCount);
        return jsonResult;
    }

    @RequestMapping("/getSingleFlow")
    @ResponseBody
    public JSONResult listSingleFlow() throws Exception {
        List<PortAndProtocol> portAndProtocols = iIpMonitoringService.listSingleFlow();
        if (portAndProtocols.size() != 0) {
            SendMail sendMail = new SendMail();
            sendMail.SendMail("发现异常单流");
        }
        JSONResult jsonResult = new JSONResult(true, "获取单流信息成功", portAndProtocols);
        return jsonResult;
    }

    @RequestMapping("/getSmurf")
    @ResponseBody
    public JSONResult listSmurf() throws Exception {
        List<PortAndProtocol> portAndProtocols = iIpMonitoringService.listSmurf();
        if (portAndProtocols.size() != 0) {
            SendMail sendMail = new SendMail();
            sendMail.SendMail("发现Smurf/Fraggle攻击");
        }
        JSONResult jsonResult = new JSONResult(true, "获取信息成功", portAndProtocols);
        return jsonResult;
    }

    @RequestMapping("/listLand")
    @ResponseBody
    public JSONResult listLand() throws Exception {
        List<PortAndProtocol> portAndProtocols = iIpMonitoringService.listLand();
        if (portAndProtocols.size() != 0) {
            SendMail sendMail = new SendMail();
            sendMail.SendMail("发现Land攻击");
        }
        JSONResult jsonResult = new JSONResult(true, "获取信息成功", portAndProtocols);
        return jsonResult;
    }

    @RequestMapping("/listFromTop")
    @ResponseBody
    public JSONResult listFromTop() {
        List<FromIpTop> fromIpTops = iIpMonitoringService.listTop();
        JSONResult jsonResult = new JSONResult(true, "以下IP可能存在攻击行为", fromIpTops);
        return jsonResult;
    }

    @RequestMapping("/getBlackmail")
    @ResponseBody
    public JSONResult getBlackmail() throws Exception {
        List<Blackmail> blackmailList = iIpMonitoringService.listBlackmail();
        int flag = 0;
        JSONResult jsonResult;
        for (Blackmail message: blackmailList) {
            if (message.getNumber()>=100) {
                flag = 1;
            }
        }
        if (flag == 0) {
            jsonResult = new JSONResult(true, "本地暂未监测到勒索病毒！", blackmailList);
        } else {
            jsonResult = new JSONResult(true, "监测到勒索病毒！", blackmailList);
            SendMail sendMail = new SendMail();
            sendMail.SendMail("发现勒索病毒，请及时处理！");
        }
        return jsonResult;
    }

    @RequestMapping("/listDns")
    @ResponseBody
    public JSONResult listDns() throws Exception {
        List<Blackmail> dns = iIpMonitoringService.listDns();
        int flag = 0;
        JSONResult jsonResult;
        for (Blackmail message: dns) {
            if (message.getNumber()>=100) {
                flag = 1;
            }
        }
        if (flag == 0) {
            jsonResult = new JSONResult(true, "本地暂未监测到DNS解析攻击！", dns);
        } else {
            jsonResult = new JSONResult(true, "监测到DNS解析攻击！", dns);
            SendMail sendMail = new SendMail();
            sendMail.SendMail("发现DNS解析攻击，请及时处理！");
        }
        return jsonResult;
    }

    /**
     * Red Code List
     *
     * @return json result
     * @throws Exception the exception
     */
    @RequestMapping("/listRedCode")
    @ResponseBody
    public JSONResult listRedCode() throws Exception {
        List<PortAndProtocol> ipMonitoringList = iIpMonitoringService.listRedCode();
        if (ipMonitoringList.size() == 0) {
            JSONResult jsonResult = new JSONResult(true, "本地电脑暂时未监测到红色代码！", ipMonitoringList);
            return jsonResult;
        } else {
            JSONResult jsonResult = new JSONResult(true, "监测到红色代码！", ipMonitoringList);
            SendMail sendMail = new SendMail();
            sendMail.SendMail("监测到红色代码!请管理员及时处理！");
            return jsonResult;
        }
    }

    @RequestMapping("/listOpasoft")
    @ResponseBody
    public JSONResult listOpasoft() throws Exception {
        List<PortAndProtocol> portAndProtocols = iIpMonitoringService.listOpasoft();
        if (portAndProtocols.size() == 0) {
            JSONResult jsonResult = new JSONResult(true, "本地电脑暂时未监测到硬盘杀手病毒！", portAndProtocols);
            return jsonResult;
        } else {
            JSONResult jsonResult = new JSONResult(true, "监测到硬盘杀手！", portAndProtocols);
            SendMail sendMail = new SendMail();
            sendMail.SendMail("监测到硬盘杀手!请管理员及时处理！");
            return jsonResult;
        }
    }

    @RequestMapping("/listSlammer")
    @ResponseBody
    public JSONResult listSlammer() throws Exception {
        List<PortAndProtocol> portAndProtocols = iIpMonitoringService.listSlammer();
        if (portAndProtocols.size() == 0) {
            JSONResult jsonResult = new JSONResult(true, "本地电脑暂时未监测到蠕虫王病毒！", portAndProtocols);
            return jsonResult;
        } else {
            JSONResult jsonResult = new JSONResult(true, "监测到蠕虫王！", portAndProtocols);
            SendMail sendMail = new SendMail();
            sendMail.SendMail("监测到蠕虫王!请管理员及时处理！");
            return jsonResult;
        }
    }

    @RequestMapping("/listShockWave")
    @ResponseBody
    public JSONResult listShockWave() throws Exception {
        List<PortAndProtocol> portAndProtocols = iIpMonitoringService.listShockWave();
        if (portAndProtocols.size() == 0) {
            JSONResult jsonResult = new JSONResult(true, "本地电脑暂时未监测到震荡波病毒！", portAndProtocols);
            return jsonResult;
        } else {
            JSONResult jsonResult = new JSONResult(true, "监测到震荡波！", portAndProtocols);
            SendMail sendMail = new SendMail();
            sendMail.SendMail("监测到震荡波!请管理员及时处理！");
            return jsonResult;
        }
    }

    @RequestMapping("/listBlaster")
    @ResponseBody
    public JSONResult listBlaster() throws Exception {
        List<PortAndProtocol> portAndProtocols = iIpMonitoringService.listBlaster();
        if (portAndProtocols.size() == 0) {
            JSONResult jsonResult = new JSONResult(true, "本地电脑暂时未监测到冲击波病毒！", portAndProtocols);
            return jsonResult;
        } else {
            JSONResult jsonResult = new JSONResult(true, "监测到冲击波！", portAndProtocols);
            SendMail sendMail = new SendMail();
            sendMail.SendMail("监测到冲击波!请管理员及时处理！");
            return jsonResult;
        }
    }

    @RequestMapping("/listWelchia")
    @ResponseBody
    public JSONResult listWelchia() throws Exception {
        List<PortAndProtocol> portAndProtocols = iIpMonitoringService.listWelchia();
        if (portAndProtocols.size() == 0) {
            JSONResult jsonResult = new JSONResult(true, "本地电脑暂时未监测到冲击波杀手病毒！", portAndProtocols);
            return jsonResult;
        } else {
            JSONResult jsonResult = new JSONResult(true, "监测到冲击波杀手！", portAndProtocols);
            SendMail sendMail = new SendMail();
            sendMail.SendMail("监测到冲击波杀手!请管理员及时处理！");
            return jsonResult;
        }
    }

    /**
     * Dos List
     *
     * @return json result
     * @throws Exception the exception
     */
    @RequestMapping("/listDos")
    @ResponseBody
    public JSONResult listDos() throws Exception {
        List<Dos> ipMonitoringList = iIpMonitoringService.listDos();
        if (ipMonitoringList.size() == 0) {
            JSONResult jsonResult = new JSONResult(true, "本地电脑暂时未监测到Dos攻击！", ipMonitoringList);
            return jsonResult;
        } else {
            JSONResult jsonResult = new JSONResult(true, "监测到Dos攻击！", ipMonitoringList);
            SendMail sendMail = new SendMail();
            sendMail.SendMail("监测到Dos攻击!请管理员及时处理！");
            return jsonResult;
        }
    }

    /**
     * DDos List
     *
     * @return json result
     * @throws Exception the exception
     */
    @RequestMapping("/listDDos")
    @ResponseBody
    public JSONResult listDDos() throws Exception {
        List<DDos> ipMonitoringList = iIpMonitoringService.listDDos();
        if (ipMonitoringList.size() == 0) {
            JSONResult jsonResult = new JSONResult(true, "本地电脑暂时未监测到DDos攻击！", ipMonitoringList);
            return jsonResult;
        } else {
            JSONResult jsonResult = new JSONResult(true, "以下IP可能存在DDos攻击", ipMonitoringList);
            return jsonResult;
        }
    }

    /**
     * Delete
     *
     * @param ip the ip
     * @return json result
     */
    @RequestMapping("/delete")
    @ResponseBody
    public JSONResult delete(String ip) {
        iIpMonitoringService.delete(ip);
        JSONResult jsonResult = new JSONResult(true, "成功删除该条IP信息!");
        return jsonResult;
    }

    /**
     * Delete d dos json result.
     *
     * @param ip the ip
     * @return the json result
     */
    @RequestMapping("/deleteDDos")
    @ResponseBody
    public JSONResult deleteDDos(String ip) {
        iIpMonitoringService.deleteDDos(ip);
        JSONResult jsonResult = new JSONResult(true, "成功删除该条IP信息!");
        return jsonResult;
    }
}